package nit.wave.sendmessage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

import nit.wave.utils.WeixinUtil;


public class Testpost {

	public static void TestPost() throws IOException {
	    String token = WeixinUtil.getAccessToken().getToken();
		System.out.println(token);
		String data = "{\"filter\":{\"group_id\":\"1\"},\"image\":{\"media_id\":\"dT-GaoNVkakuCuYxxkVapty_1lUCq5cpenVLfbfrPrSo2OqrDuaKM_MU8btMJKQp\"},\"msgtype\":\"image\"}";
		String urlad = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN";
		URL url = new URL(urlad.replace("ACCESS_TOKEN", token));
		URLConnection connection = url.openConnection();
		connection.setDoOutput(true);
		OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
		out.write(data); // 向页面传递数据。post的关键所在！
		out.flush();
		out.close();
		// 一旦发送成功，用以下方法就可以得到服务器的回应：
		String sCurrentLine = "";
		String sTotalString = "";
		InputStream l_urlStream = connection.getInputStream();
		// 传说中的三层包装阿！
		BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
		while ((sCurrentLine = l_reader.readLine()) != null) {
			sTotalString += sCurrentLine + "\r\n";

		}
		System.out.println(sTotalString);

	}

	public static void main(String[] args) throws IOException {
		TestPost();
	}
}